package com.genesis.yunnanji.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

public class Land extends Fragment  implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    private View view;
    private GenesisUtils genesisUtils;
    private WebView mWebView;
    private ProgressBar mProgressBar;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private Toolbar mToolbar;
    private String url;
    private String id;
    private RelativeLayout share;

    public static Land newInstance(String param) {
        Land obj = new Land();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.activity_webactivity, null);
//            StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.main_color), 0);
            genesisUtils = GenesisUtils.getIntance();
            url = getArguments().getString("args1");
            share = (RelativeLayout) view.findViewById(R.id.bt_web_share);
            mWebView = (WebView) view.findViewById(R.id.wv_web_webview);
            mProgressBar = (ProgressBar) view.findViewById(R.id.pb_web_progress);
            mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.sr_web_swipe);
            mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.main_color));
            mToolbar = (Toolbar) view.findViewById(R.id.tb_web_toolbar);
            mToolbar.setBackgroundColor(getResources().getColor(R.color.main_color));
            mToolbar.setTitle("");
            setHasOptionsMenu(true);
            ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            share.setOnClickListener(this);
            mWebView.loadUrl(url);
            mSwipeRefreshLayout.setOnRefreshListener(this);
            mProgressBar.setMax(100);
            setUpWebview();
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    }
                }
            });
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    private void setUpWebview() {
        mWebView.setWebChromeClient(new MyWebChromeClient());
        mWebView.setWebViewClient(new mWebClient());
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setDefaultFontSize(12);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setGeolocationEnabled(true);
        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        if (Build.VERSION.SDK_INT >= 21) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    @Override
    public void onRefresh() {
        mWebView.loadUrl(mWebView.getUrl());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_web_share:
                UMImage thumb = new UMImage(getActivity(), R.mipmap.sharelog);
                String[] str = mWebView.getUrl().split("__apptoken__");
                String shareUrl = str[0];
                UMWeb web = new UMWeb(shareUrl);
                web.setTitle(mWebView.getTitle());//标题
                web.setThumb(thumb);
                web.setDescription(mWebView.getTitle());
                new ShareAction(getActivity())
                        .withMedia(web)
                        .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SMS, SHARE_MEDIA.WEIXIN_FAVORITE, SHARE_MEDIA.EMAIL)
                        .open();
                break;
        }
    }

    String webTitle;

    private class MyWebChromeClient extends WebChromeClient {


        @Override
        public void onReceivedTitle(WebView view, String title) {
            // TODO Auto-generated method stub
            mToolbar.setTitle(title);
            webTitle = title;

        }


        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mProgressBar.setVisibility(View.GONE);
                mSwipeRefreshLayout.setRefreshing(false);

            } else {
                if (mProgressBar.getVisibility() == View.GONE) {
                    mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
                if (!mSwipeRefreshLayout.isRefreshing()) {
                    mSwipeRefreshLayout.setRefreshing(true);
                }
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    private class mWebClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(Build.VERSION.SDK_INT<26) {
                view.loadUrl(url);
                return true;
            }
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            share.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);
        }
    }



    private void settext() {
        String title = "";
        mWebView.getId();
    }

}
